<?php
namespace Home\Controller;
class StatisticalController extends CommonController {
	// 统计数据
	public function data()
	{
		$this->Is_power("money");        //检测用户权限,如果错误直接返回错误,终止程序
		$data = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
		// 今日0点时间戳
		$time = strtotime(date("Y-m-d"),time());

		// 查询当前主播用户信息
		$memberData = M('member')->field('status,sex,creationtime')->select();
		foreach ($memberData as &$vo) {
			// 主播总人数
			if ($vo['status'] == 9)
				$data[0] += 1;
			// 男主播总数
			if ($vo['sex'] == 1 && $vo['status'] == 9)
				$data[1] += 1;
			// 女主播总数
			if ($vo['sex'] == 2 && $vo['status'] == 9)
				$data[2] += 1;
			// 今日注册人数
			if (strtotime($vo['creationtime']) > $time)
				$data[3] += 1;
		}

		// 获取当前结算人数和金额
		$balancecount = M('balance')->field('count(*)')->group('uuid')->where("time >= {$time}")->select();
		$balancesum = M('balance')->where("time >= {$time}")->sum('amount');
		$data[4] += count($balancecount);
		$data[5] += $balancesum;

		// 获取当前订单数据
		$orderData = M('theorder')->field('state,isdummy,newsingle,money')->where("releasetime >= {$time}")->select();
		foreach ($orderData as &$vo) {
			// 真实订单总数  应付金额
			if ($vo['isdummy'] == 0)
				$data[6] += 1;
				$data[12] += $vo['money'];
			// 超时订单
			if ($vo['isdummy'] == 0 && $vo['state'] == 7)
				$data[7] += 1;
			// 主动取消订单
			if ($vo['isdummy'] == 0 && $vo['state'] == 8)
				$data[8] += 1;
			// 新人单总数
			if ($vo['newsingle'] == 1)
				$data[9] += 1;
			// 虚拟订单数
			if ($vo['isdummy'] == 1)
				$data[10] += 1;
			// 实付金额
			if ($vo['isdummy'] == 0 && $vo['state'] == 4){
				$data[13] += $vo['money'];
			}
		}

		// 今日多少主播参与抢单
		$getorder = M('theorderinfo')->field('count(*)')->group('uuid')->where("receivingtime >= {$time}")->select();
		$data[10] += count($getorder);

		// 虚拟单抢单总次数
		$xnorder = M('theorder')->where("isdummy = 1 AND releasetime >= {$time}")->field('id')->select();
		foreach ($xnorder as &$vo) {
			$data[11] += M('theorderinfo')->where("(status = 2 OR status = 3) AND oid = ".$vo['id'])->count();
		}

		// 今日抢单总次数
		$data[14] = M('theorderinfo')->where("(status = 2 OR status = 3) AND receivingtime >= {$time}")->count();

		// 今日新人单抢单总人数
		$neworder = M('theorder')->where("newsingle = 1 AND releasetime >= {$time}")->field('id')->select();
		$neworderdata = [];
		foreach ($neworder as &$vo) {
			$ndata = M('theorderinfo')->where("status = 2 AND oid = ".$vo['id'])->field('uuid')->select();
			foreach ($ndata as &$vo) {
				array_push($neworderdata, $vo['uuid']);
			}
		}
		$neworderdata = array_unique($neworderdata);
		$data[15] = count($neworderdata);

		// 计算虚拟单抢单人数
		$neworder = M('theorder')->where("isdummy = 1 AND releasetime >= {$time}")->field('id')->select();
		$neworderdata = [];
		foreach ($neworder as &$vo) {
			$ndata = M('theorderinfo')->where("(status = 2 OR status = 3) AND oid = ".$vo['id'])->field('uuid')->select();
			foreach ($ndata as &$vo) {
				array_push($neworderdata, $vo['uuid']);
			}
		}
		$neworderdata = array_unique($neworderdata);
		$data[16] = count($neworderdata);

		$this->assign('data',$data);
		$this->display();
	}
}


